#include "Item.h"

namespace Xps
{
	Item::Item(XpsAssets::TreeNode::NodeType type) : QGraphicsItem()
	{
		this->type = type;
		pen.setBrush(QBrush(Qt::SolidPattern));
		pen.setWidth(3.75);
		pen.setStyle(Qt::SolidLine);
		pen.setCapStyle(Qt::FlatCap);
		pen.setJoinStyle(Qt::MiterJoin);
			selectionpen.setWidth(1.75);
			selectionpen.setStyle(Qt::DashLine);
			selectionpen.setColor(Qt::gray);
		font = QFont("Arial", 16, QFont::PreferAntialias | QFont::PreferQuality);
		//setZValue((x + y) % 2);
		size.setHeight(80);
		size.setWidth(80);
		setPos(0,0);
		setFlags(ItemIsSelectable | ItemIsMovable);
		setAcceptsHoverEvents(true);
		nodemarker = 0;
	}

	QRectF Item::boundingRect() const
	{
		return QRectF(-3, -3, size.width()+6, size.height()+6);
	}
	
	void Item::paintSelectionRect(QPainter *painter)
	{
		QRectF quadShape(QPointF(0,0),size);

		painter->setPen(selectionpen);
		painter->setBrush(QBrush(Qt::transparent));
		painter->drawRoundRect(quadShape, 50,50);
	}

	void Item::paintHoverRect(QPainter *painter)
	{
		QRectF quadShape(QPointF(0,0),size);

		painter->setPen(QPen(Qt::transparent));
		painter->setBrush(QBrush(Qt::lightGray,Qt::SolidPattern));
		painter->drawRoundRect(quadShape, 50,50);
	}

	void Item::markTrue()
	{
		nodemarker = 1;
		update();
	}

	void Item::markFalse()
	{
		nodemarker = 2;
		update();
	}
	void Item::markNone()
	{
		nodemarker = 0;
		update();
	}
	void Item::drawTrueMarker(QPainter *painter)
	{
		painter->setPen(QPen(Qt::white));
		painter->setBrush(QBrush(Qt::darkGreen));
		painter->drawEllipse(QRectF(0,0,10,10));
	}

	void Item::drawFalseMarker(QPainter *painter)
	{
		painter->setPen(QPen(Qt::white));
		painter->setBrush(QBrush(Qt::darkRed));
		painter->drawEllipse(QRectF(0,0,10,10));
	}

	void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
	{
		painter->setClipRect(option->exposedRect);

		if (nodemarker == 1)
			drawTrueMarker(painter);
		else if (nodemarker == 2)
			drawFalseMarker(painter);
	} 
	
	void Item::mousePressEvent(QGraphicsSceneMouseEvent *event)
	{
		if (event->button() == Qt::LeftButton)
		{
			QGraphicsItem::mousePressEvent(event);
			update();
		}
	}

	void Item::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
	{
		QList<QGraphicsItem*> selecteditems = this->scene()->selectedItems();
		QGraphicsItem::mouseMoveEvent(event);
		if (selecteditems.count() > 0)
		{
			for (int i = 0; i < selecteditems.count(); i++)
			{
				int targetx = selecteditems.at(i)->scenePos().x();
				int targety = selecteditems.at(i)->scenePos().y();
				selecteditems.at(i)->setPos(targetx, targety);
			}
		}
		update();
	}
	
	QWidget* Item::getWidget()
	{
		return 0;
	}

	void Item::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
	{
		QList<QGraphicsItem*> selecteditems = this->scene()->selectedItems();

		if (selecteditems.count() > 0)
		{
			for (int i = 0; i < selecteditems.count(); i++)
			{
				// SNAP TO GRID
				// make it bett0r
				int targetx = selecteditems.at(i)->scenePos().x();
				int targety = selecteditems.at(i)->scenePos().y();
				int offsetx = ((int) selecteditems.at(i)->scenePos().x()) % 20;
				int offsety = ((int) selecteditems.at(i)->scenePos().y()) % 20;
				
				if (offsetx > 10 && targetx > 0)
					offsetx -= 20; 
				else if	(offsetx < -10 && targetx < 0)
					offsetx += 20;
				
				if (offsety > 10 && targety > 0) 
					offsety -= 20;
				else if	(offsety < -10 && targety < 0) 
					offsety += 20;

				if (targetx < 0)
					targetx += (20-offsetx-20); 
				else
					targetx -= offsetx;
				
				if (targety < 0) 
					targety += (20-offsety-20);
				else
					targety -= offsety;

				selecteditems.at(i)->setPos(targetx, targety);
			}
		}
		if (event->button() == Qt::LeftButton)
		{
			QGraphicsItem::mouseReleaseEvent(event);
			update();
		}
	}
}

